package com.example.demo.service;

import com.example.demo.entity.Student;
import com.example.demo.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class StudentService {
    @Autowired
    StudentRepository studentRepository;
    public Student save(Student student){
     return studentRepository.save(student);
    }
    public void deleteById(Integer id){
        studentRepository.deleteById(id);
    }
    public Student findById(Integer id){
        return studentRepository.findById(id).orElse(null);
    }
    public List<Student> findAll(){
        return studentRepository.findAll();
    }
    public Student insert(Student student){
        student.setId(03);
        studentRepository.save(student);
        return  student;
    }
    public Student update(Student student){
        Optional<Student> optionalStudent=studentRepository.findById(student.getId());
        if(optionalStudent.isPresent()){
            studentRepository.save(student);
            return  student;
        }else{
            return null;
        }
    }


}
